package com.weichuanbo.xwdz.mvp.presenter;

import android.app.Application;
import android.content.Intent;
import android.text.TextUtils;

import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.RxLifecycleUtils;
import com.weichuanbo.xwdz.R;
import com.weichuanbo.xwdz.app.Common;
import com.weichuanbo.xwdz.app.utils.SPUtil;
import com.weichuanbo.xwdz.app.utils.SaveBeanUtil;
import com.weichuanbo.xwdz.im.SealAppContext;
import com.weichuanbo.xwdz.im.SealUserInfoManager;
import com.weichuanbo.xwdz.mvp.contract.UserContract;
import com.weichuanbo.xwdz.mvp.model.entity.UserBean;
import com.weichuanbo.xwdz.mvp.ui.activity.MainActivity;
import com.weichuanbo.xwdz.mvp.ui.activity.PhoneLoginActivity;
import com.weichuanbo.xwdz.mvp.ui.activity.SelecteLoginActivity;

import java.util.logging.LogManager;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.rong.imkit.RongIM;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

@ActivityScope
public class UserPresenter extends BasePresenter<UserContract.Model, UserContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;
    /**
     * 未完善用户信息
     */
    private String IMPERFECT="1";

    @Inject
    public UserPresenter(UserContract.Model model, UserContract.View rootView) {
        super(model, rootView);
    }

    /**
     * 获取验证码
     * @param phone
     */
    public void getCode(String phone) {
        mModel.getCode(phone)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                //使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<Object>(mErrorHandler) {
                    @Override
                    public void onNext(Object callback) {
                        Intent intent = new Intent(mApplication, PhoneLoginActivity.class)
                                .putExtra("phone", phone);
                        mRootView.launchActivity(intent);

                    }
                });
    }

    /**
     * 登陆
     * @param phone
     * @param verifycode
     */
    public void login(String phone,String verifycode) {
       /* if (!isAgreeProtocol) {
            DisplayUtil.displayShortToast(this, "请同意用户协议!");//同意用户协议
            return;
        }*/
        if (TextUtils.isEmpty(verifycode)){
            mRootView.showMessage(mApplication.getString(R.string.please_input_code));
            return;
        }
        //md 手机登陆
        mModel.login(phone,verifycode,"mb")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                //使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<UserBean>(mErrorHandler) {
                    @Override
                    public void onNext(UserBean userBean) {
                        //保存用户信息
                        SaveBeanUtil.putBean(mApplication, Common.Const.VIPINFO, userBean);
                        //跳转到完善资料页面
                        if (!IMPERFECT.equals(userBean.getInfo_status())) {
                            /*new Intent(LoginActivity.this, SupplementActivity.class)
                                    .putExtra("account",loginBean.getAccount())
                                    .putExtra("type","mb");*/
                            //直接登陆
                        }else {
                            try {
//                                SPUtil.put(getApplicationContext(), Common.Const.IM_TOKEN, loginBean.getRongcloud_token());
//                                SealUserInfoManager.getInstance().openDB();
                                //登陆im
//                                BroadcastManager.getInstance(getApplication()).sendBroadcast(Common.Const.DROP_LINE);login

                                //登陆im
                                SealAppContext.getInstance().loginIm(userBean.getRongcloud_token());
                                Intent intent = new Intent(mApplication, MainActivity.class);
                                mRootView.launchActivity(intent);
                            mRootView.killMyself();
                            } catch (Exception e) {

                            }
                        }
                        mAppManager.killActivity(SelecteLoginActivity.class);
                        mAppManager.killActivity(PhoneLoginActivity.class);
                        mRootView.killMyself();
                    }
                });
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }


}
